﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yiCommerce.Areas.Admin.Models.Orders;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Orders;
using yiCommerce.Core.Domain.Payments;
using yiCommerce.Core.Domain.Securitys;
using yiCommerce.Core.Domain.Shippings;
using yiCommerce.Core.Domain.Tradings;
using yiCommerce.Models.Orders;
using yiCommerce.Service.Orders;
using yiCommerce.Service.Payments;
using yiCommerce.Service.Tradings;

namespace yiCommerce.Controllers
{
    public class OrderController : Controller
    {

        private IOrderService _orderService;
        private IWorkContext _workContext;
        private IPaymentService _paymentService;
        private ITradingService _tradingService;

        public OrderController(IOrderService orderService, IWorkContext workContext, IPaymentService paymentService, ITradingService tradingService)
        {
            _orderService = orderService;
            _workContext = workContext;
            _paymentService = paymentService;
            _tradingService = tradingService;
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }



        private List<SelectItem> ConvertOrderStatusToSelectItemList(IEnumerable<OrderStatus> orderStatuses)
        {
            return orderStatuses.Select(orderstatus => new SelectItem()
            {
                Value = (int)orderstatus,
                Text = orderstatus.ToDesc()
            }).ToList();
        }


        private List<SelectItem> ConvertPaymentStatusToSelectItemList(IEnumerable<PaymentStatus> paymentStatus)
        {
            return paymentStatus.Select(p => new SelectItem()
            {
                Value = (int)p,
                Text = p.ToDesc()
            }).ToList();
        }
        private List<SelectItem> ConvertShippingStatusToSelectItemList(IEnumerable<ShippingStatus> shippingStatus)
        {
            return shippingStatus.Select(s => new SelectItem()
            {
                Value = (int)s,
                Text = s.ToDesc()
            }).ToList();
        }


        public ActionResult RePostPayment(int orderId)
        {
            var order = _orderService.GetOrderById(orderId);
            if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
                return new HttpUnauthorizedResult();

            if (!_paymentService.CanRePostProcessPayment(order))
                return RedirectToRoute("OrderDetails", new { orderId = orderId });

            var trading = this._tradingService.GenerateNewTrading();
            trading.Orders.Add(new TradingOrder(){OrderId = orderId, TradingId =  trading.Id});

            trading.PaymentMethodSystemName = order.PaymentMethodSystemName;
            _tradingService.Update(trading);


            var postProcessPaymentRequest = new PostProcessPaymentRequest()
            {
                Trading = trading,
                PaymentMethodSystemName = order.PaymentMethodSystemName,
            };
            _paymentService.PostProcessPayment(postProcessPaymentRequest);
            return null;

            //if (_webHelper.IsRequestBeingRedirected || _webHelper.IsPostBeingDone)
            //{
            //    //redirection or POST has been done in PostProcessPayment
            //    return Content("Redirected");
            //}
            //else
            //{
            //    //if no redirection has been done (to a third-party payment page)
            //    //theoretically it's not possible
            //    return RedirectToRoute("OrderDetails", new { orderId = orderId });
            //}
        }

        public ActionResult Confirm(int orderId)
        {
            var confirmOrderModel = new ConfirmOrderModel()
                {

                    OrderId =  orderId
                };
            ViewBag.Model = confirmOrderModel;
            return View();
        }

      
        public ActionResult List()
        {


            var model = new OrderSearchFilterModel()
            {
                OrderStatus = OrderStatus.Processing,
                OrderStatusId = (int)OrderStatus.Processing,
                PaymentStatus = PaymentStatus.Pending,
                PaymentStatusId = (int)PaymentStatus.Pending,
                ShippingStatus = ShippingStatus.NotYetShipped,
                ShippingStatusId = (int)ShippingStatus.NotYetShipped,
                AvailableOrderStatuses = ConvertOrderStatusToSelectItemList(Enum.GetValues(typeof(OrderStatus)).Cast<OrderStatus>()),
                AvailablePaymentStatuses = ConvertPaymentStatusToSelectItemList(Enum.GetValues(typeof(PaymentStatus)).Cast<PaymentStatus>()),
                AvailableShippingStatuses = ConvertShippingStatusToSelectItemList(Enum.GetValues(typeof(ShippingStatus)).Cast<ShippingStatus>()),
            };



            //load orders
            var pageIndex = 0;
            int pageSize = 100;
            var orders = _orderService.SearchOrders(
                    _workContext.CurrentCustomer.Id, 0,
                    model.StartDate,
                    model.EndDate,
                    model.OrderStatus,
                    model.PaymentStatus,
                    model.ShippingStatus,
                    pageIndex,
                    pageSize);

            var resultModel = new OrderSearchModel { Orders = orders, Filter = model };
            ViewBag.Model = resultModel;


            return View();

        }
    }
}
